import time

a = 100
c = 200
def f1():
    b = 3
    a = 4
    global c
    print(c)
    c = 300
    print(a+b)
    # 输出所有的局部和全局变更
    print("局部:", locals())
    print("指定:", vars())
    print("全局:", globals())

f1()
print(a)
print(c)
## vars,现返回对象object的属性和属性值的字典对象, 不传参数就=locals
## locals表示当前生效作用域的变量，返回的是当前所在最小命名空间的局部变量的一个拷贝
print("局部:", locals())
print("指定:", vars(f1))
print("全局:", globals())


# 局部变量引用的时间比较快；全局变量引用的时间比较慢;尽量用局部变量
aa = 1000
def test01():
    print("aa",aa)
    start = time.time()
    global a
    for i in range(100000000):
        a += 1
    end = time.time()
    print("全局耗时{0}".format(end-start))
def test02():
    start = time.time()
    cc = 1000
    for i in range(100000000):
        cc += 1
    end = time.time()
    print("局部耗时{0}".format(end-start))


test01();test02()